/**
 * @package: interfaz
 * @class: 
 *
 * @version: 1.0
 */
package cfgs.gestudent.finestra.main;

/**
 * @librerias importadas
 *
 */
import com.griaule.grfingerjava.GrFingerJavaException;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;

import cfgs.gestudent.controller.lector.*;
import cfgs.gestudent.preferencies.ConfiguracionsIdiomes;

import javax.swing.JLabel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;

import java.awt.Color;

public class RegistrarAssistenciaLector extends javax.swing.JFrame {
	
	
	/**
	 * @atributos
	 *
	 * @access: private
	 */
	// objecto usado para realizar todas las operaciones relacionadas al
	// Fingerprint-SDK
	private FuncionsLectorRegistre procedimientosSDK;
	// Panel para mostrar la huella digital
	private JPanel fingerprintViewPanel = null;
	// Imagen de la huella actual
	private BufferedImage fingerprintImage = null;
	
	
	/**
	 * ResourceBundle
	 */
	private ConfiguracionsIdiomes idioma = new ConfiguracionsIdiomes();
	private ResourceBundle rb = idioma.getIdioma();

	/**
	 * @function: 
	 *
	 * @access: public
	 * @return
	 */
	public RegistrarAssistenciaLector() {
		inicializar();
		initComponents();
		procedimientosSDK.inicializarCaptura();
		setLocationRelativeTo(null);
		setVisible(true);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				procedimientosSDK.finalizarCaptura();
			}
		});
		
	}

	/**
	 * @function: inicializar
	 *
	 * @access: public
	 * @return
	 */
	public void inicializar() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, rb.getString("UIManagerError")+" lector \n "+e);
		}
		/**
		 * favicon
		 */
		this.setIconImage(new ImageIcon("img/favicon.jpg").getImage());
		// Crea una instancia de Util
		this.procedimientosSDK = new FuncionsLectorRegistre(this);
	}

	/**
	 * @function: crearPanelHuella
	 *
	 * @access: private
	 * @return
	 */
	private JComponent crearPanelHuella() {
		// Crea un panel nuevo para mostrar la huella
		fingerprintViewPanel = new JPanel() {
			// Se sobreescribe el método paintComponent para habilitar la
			// muestra de la imagen de la huella
			public void paintComponent(Graphics g) {
				super.paintComponent(g);
				// Si hay una imagen para ser mostrada
				if (fingerprintImage != null) {
					// Calcula el tamaño y posición de la imagen para ser
					// pintada el tamaño es ajustado para que ocupe todo el
					// tamaño del panel
					Insets insets = getInsets();
					int transX = insets.left;
					int transY = insets.top;
					int width = getWidth() - getInsets().right
							- getInsets().left;
					int height = getHeight() - getInsets().bottom
							- getInsets().top;
					// Se dibuja la imagen
					g.drawImage(fingerprintImage, transX, transY, width,
							height, null);
				}
			}
		};
		// Se agrega un border alrededor del panel
		fingerprintViewPanel.setBorder(new CompoundBorder(new EmptyBorder(2, 2,
				2, 2), new BevelBorder(BevelBorder.LOWERED)));
		fingerprintViewPanel
				.setToolTipText(rb.getString("capturadaToolTip"));
		if (fingerprintViewPanel == null) {
			System.exit(0);
			return null;
		} else {
			return fingerprintViewPanel;
		}
	}

	/**
	 * @function: showImage
	 *
	 * @access: public
	 * @return
	 */
	public void showImage(BufferedImage image) {
		// Utiliza el imageProducer para crear una imagen de la huella digital
		fingerprintImage = image;
		// Se dibuja la nueva imagen
		repaint();
	}

	/**
	 * @function: initComponents
	 *
	 * @access: private
	 * @return
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jPanel2 = new javax.swing.JPanel();
		btnIdentificar = new javax.swing.JButton();
		btnGuardar = new javax.swing.JButton();
		btnSalir = new javax.swing.JButton();
		PanelContenedor = new javax.swing.JPanel();
		
		setTitle(rb.getString("titolRegistraAssistencia"));
//		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setResizable(false);
		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowActivated(java.awt.event.WindowEvent evt) {
				formWindowActivated(evt);
			}

			public void windowClosing(java.awt.event.WindowEvent evt) {
				formWindowClosing(evt);
			}
		});

		jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(
				new javax.swing.border.SoftBevelBorder(
						javax.swing.border.BevelBorder.RAISED), rb.getString("titolPanelBotons"),
				javax.swing.border.TitledBorder.CENTER,
				javax.swing.border.TitledBorder.DEFAULT_POSITION));

		btnIdentificar.setText(rb.getString("btnIdentificar"));
		btnIdentificar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnIdentificarActionPerformed(evt);
			}
		});

		btnGuardar.setText(rb.getString("btnGuardar"));
		btnGuardar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnGuardarActionPerformed(evt);
			}
		});

		btnSalir.setText(rb.getString("btnSortir"));
		btnSalir.setAlignmentY(0.0F);
		btnSalir.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnSalirActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout
				.setHorizontalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																jPanel2Layout
																		.createSequentialGroup()
																		.addGap(0,
																				0,
																				Short.MAX_VALUE)
																		.addComponent(
																				btnSalir,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				124,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(100,
																				100,
																				100)))));
		jPanel2Layout
				.setVerticalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						
										.addComponent(
												btnSalir,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												35,
												javax.swing.GroupLayout.PREFERRED_SIZE));

		PanelContenedor.setBorder(javax.swing.BorderFactory.createTitledBorder(
				null, rb.getString("titolBorderEmpremta"), javax.swing.border.TitledBorder.CENTER,
				javax.swing.border.TitledBorder.DEFAULT_POSITION));
		PanelContenedor.setLayout(new java.awt.BorderLayout());
		
		lblInforegistre = new JLabel("");
		lblInforegistre.setForeground(Color.RED);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		layout.setHorizontalGroup(
			layout.createParallelGroup(Alignment.TRAILING)
				.addGroup(layout.createSequentialGroup()
					.addGap(0, 0, Short.MAX_VALUE)
					.addComponent(PanelContenedor, GroupLayout.PREFERRED_SIZE, 364, GroupLayout.PREFERRED_SIZE))
				.addGroup(layout.createSequentialGroup()
					.addGap(10)
					.addComponent(jPanel2, GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
					.addContainerGap())
				.addGroup(Alignment.LEADING, layout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblInforegistre, GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE)
					.addContainerGap())
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, layout.createSequentialGroup()
					.addComponent(PanelContenedor, GroupLayout.PREFERRED_SIZE, 353, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(jPanel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addComponent(lblInforegistre))
		);
		getContentPane().setLayout(layout);

		PanelContenedor.getAccessibleContext().setAccessibleParent(jPanel2);

		pack();
	}// </editor-fold>

	/**
	 * @function: btnGuardarActionPerformed
	 *
	 * @access: private
	 * @return
	 */
	private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {
		try {
			// procedimientosSDK.guardarHuella();
			// procedimientosSDK.guardarHuellaBackup();
			procedimientosSDK.guardar();
		} catch (GrFingerJavaException ex) {
			Logger.getLogger(RegistrarAssistenciaLector.class.getName()).log(
					Level.SEVERE, null, ex);
		}
	}

	/**
	 * @function: btnSalirActionPerformed
	 *
	 * @access: private
	 * @return
	 */
	private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {
		int result = JOptionPane.showConfirmDialog((Component) evt.getSource(),
        		rb.getString("eventCloseWindowMissatgeReg"),rb.getString("eventCloseWindow"), JOptionPane.YES_NO_CANCEL_OPTION);
            if (result == JOptionPane.YES_OPTION){
            	procedimientosSDK.finalizarCaptura();
            	dispose();

            }
		
	}

	/**
	 * @function: formWindowActivated
	 *
	 * @access: private
	 * @return
	 */
	private void formWindowActivated(java.awt.event.WindowEvent evt) {
		this.PanelContenedor.add(this.crearPanelHuella());
	}

	/**
	 * @function: formWindowClosing
	 *
	 * @access: private
	 * @return
	 */
	private void formWindowClosing(java.awt.event.WindowEvent evt) {
		procedimientosSDK.finalizarCaptura();
	}

	/**
	 * @function: btnIdentificarActionPerformed
	 *
	 * @access: private
	 * @return
	 */
	private void btnIdentificarActionPerformed(java.awt.event.ActionEvent evt) {
		try {
			procedimientosSDK.identificarPersona();
		} catch (GrFingerJavaException ex) {
			Logger.getLogger(RegistrarAssistenciaLector.class.getName()).log(
					Level.SEVERE, null, ex);
		}
	}

	/**
	 * @atributos
	 *
	 * @access: private
	 */
	// Variables declaration - do not modify
	private javax.swing.JPanel PanelContenedor;
	private javax.swing.JButton btnGuardar;
	private javax.swing.JButton btnIdentificar;
	private javax.swing.JButton btnSalir;
	private javax.swing.JPanel jPanel2;
	private JLabel lblInforegistre;

	public JLabel getLblInforegistre() {
		return lblInforegistre;
	}

	public void setLblInforegistre(JLabel lblInforegistre) {
		this.lblInforegistre = lblInforegistre;
	}
	
	
}